<?php
/**
 * Created by PhpStorm.
 * User: 68008000
 * Date: 2017/11/15
 * Time: 14:37
 */

namespace app\common\model;


use think\Model;
use think\Validate;

class Article extends Model
{
    //连接数据库
    protected $table= 'article';
    protected $pk= 'aid';
    //数据完成,自动更新
    protected $auto = ['user_uid'];
    protected $insert = ['sendtime']; //添加时间
//    protected $update = ['login_ip'];//修改时间
    protected function setUserUidAttr()
    {
        return session('admin.uid');
    }
    protected function setSendTimeAttr()
    {
        return time();
    }
    //添加文章
    public function add($data){
        //验证文章数据表
        $validate = new Validate([
            'keywords' => 'require',
            'des' => 'require',

        ],[
            'keywords.require' => '请填写关键字',
            'des.require' => '请填写文章描述',

        ]);
        if (!$validate->check($data)) {
            return ['valid' => 0, 'msg' => $validate->getError()];
        }

        //验证文章标签中间表
        if(!isset($data['tag_tid'])){
            return ['valid'=>0,'msg'=>'请选择标签'];
        }
        //1添加文章数据库
        $result = $this->validate(true)->allowField(true)->save($data);
        if(false === $result){
            // 验证失败 输出错误信息
            return ['valid'=>0,'msg'=>$this->getError()];
        }else{
            //3.文章标签中间表添加
            foreach ($data['tag_tid'] as $k=>$v){
                $ArtTag =new ArtTag([
                    'article_aid'=>$this->aid,
                    'tag_tid'=>$v
                ]);
                $ArtTag->save();
            }
            //2文章内容保存
            $ArtData = new ArtData([
                'keywords'  =>  $data['keywords'],
                'des' => $data['des'],
                'content'=>$data['editorValue'],
                'article_aid'=>$this->aid
            ]);
            $ArtData->save();
            return ['valid'=>1,'msg'=>'添加成功'];
        }
    }
    //修改文章
    public function edit($data){
        $aid = input('param.aid');
        $article = new article;
        // save方法第二个参数为更新条件
        $res=$article->allowField(true)->save($data,['aid' => $aid]);
        if(false === $res){
            // 验证失败 输出错误信息
            return ['valid'=>0,'msg'=>$this->getError()];
        }else{
            //修改标签
            ArtTag::where('article_aid',$aid)->delete();
            foreach ($data['tag_tid'] as $k=>$v) {
                $ArtTag = new ArtTag([
                    'article_aid' => $aid,
                    'tag_tid' => $v
                ]);
                $ArtTag->save();
            }
            //修改文章内容
            $article = new ArtData;
            // save方法第二个参数为更新条件
           $article->save([
               'keywords'  =>  $data['keywords'],
               'des' => $data['des'],
               'content'=>$data['content'],
           ],['article_aid' => $aid]);
            return ['valid'=>1,'msg'=>'修改成功'];
        }
    }

    //放入回收站
    public function recycle($aid){
        db('article')->where('is_recycle',1)->find();
        $res=db('article')->where('aid',$aid)->setField('is_recycle','0');
        if ($res){
            return ['valid'=>1,'msg'=>'移除成功'];
        }else{
            return ['valid'=>0,'msg'=>'修改失败'];
        }

    }
    //还原
    public function reduction($aid){
       db('article')->where('is_recycle',0)->find();
        $res=db('article')->where('aid',$aid)->setField('is_recycle','1');
        if ($res){
            return ['valid'=>1,'msg'=>'还原成功'];
        }else{
            return ['valid'=>0,'msg'=>'修改失败'];
        }
    }
    //删除
    public function del($aid){
        $res=$this::where('aid',$aid)->delete();
        if(false === $res){
            // 验证失败 输出错误信息
            return ['valid'=>0,'msg'=>$this->getError()];
        }else{
            ArtTag::where('article_aid',$aid)->delete();
            ArtData::Where('article_aid',$aid)->delete();
            return ['valid'=>1,'msg'=>'删除成功'];
        }
    }

}